package org.jeecg.modules.online.cgform.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/* renamed from: org.jeecg.modules.online.cgform.d.e */
/* loaded from: hibernate-re-3.7.1-RC.jar:org/jeecg/modules/online/cgform/d/e.class */
public class FileDownloadUtils {

    /* renamed from: a */
    private static final Logger f316a = LoggerFactory.getLogger(FileDownloadUtils.class);

    /* renamed from: a */
    public static void m290a(HttpServletResponse httpServletResponse, String str, String str2) {
        httpServletResponse.setCharacterEncoding("UTF-8");
        File file = new File(str);
        if (!file.exists()) {
            throw new NullPointerException("Specified file not found");
        }
        if (str2 == null || str2.isEmpty()) {
            throw new NullPointerException("The file name can not null");
        }
        httpServletResponse.setHeader("content-type", "application/octet-stream");
        httpServletResponse.setContentType("application/octet-stream");
        try {
            httpServletResponse.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(str2, "UTF-8"));
            httpServletResponse.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        } catch (UnsupportedEncodingException e) {
            f316a.error(e.getMessage(), e);
        }
        byte[] bArr = new byte[1024];
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
            Throwable th = null;
            try {
                ServletOutputStream outputStream = httpServletResponse.getOutputStream();
                for (int read = bufferedInputStream.read(bArr); read != -1; read = bufferedInputStream.read(bArr)) {
                    outputStream.write(bArr, 0, read);
                }
                if (bufferedInputStream != null) {
                    if (0 != 0) {
                        try {
                            bufferedInputStream.close();
                        } catch (Throwable th2) {
                            th.addSuppressed(th2);
                        }
                    } else {
                        bufferedInputStream.close();
                    }
                }
                if (fileInputStream != null) {
                    if (0 != 0) {
                        fileInputStream.close();
                    } else {
                        fileInputStream.close();
                    }
                }
            } catch (Throwable th3) {
                try {
                    throw th3;
                } catch (Throwable th4) {
                    if (bufferedInputStream != null) {
                        if (th3 != null) {
                            try {
                                bufferedInputStream.close();
                            } catch (Throwable th5) {
                                th3.addSuppressed(th5);
                            }
                        } else {
                            bufferedInputStream.close();
                        }
                    }
                    throw th4;
                }
            }
        } catch (Exception e2) {
            f316a.error(e2.getMessage(), e2);
        }
    }

    /* renamed from: a */
    public static void m289a(HttpServletResponse httpServletResponse, List<String> list, String str) throws IOException {
        String str2 = str + ".zip";
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setHeader("content-type", "application/octet-stream");
        httpServletResponse.setContentType("application/octet-stream");
        httpServletResponse.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(str2, "UTF-8"));
        httpServletResponse.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        try {
            ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(httpServletResponse.getOutputStream()));
            DataOutputStream dataOutputStream = new DataOutputStream(zipOutputStream);
            Throwable th = null;
            try {
                zipOutputStream.setMethod(8);
                for (String str3 : list) {
                    File file = new File(str3);
                    if (file.exists()) {
                        zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
                        InputStream newInputStream = Files.newInputStream(file.toPath(), new OpenOption[0]);
                        byte[] bArr = new byte[1024];
                        while (true) {
                            int read = newInputStream.read(bArr);
                            if (read == -1) {
                                break;
                            }
                            dataOutputStream.write(bArr, 0, read);
                        }
                        newInputStream.close();
                        zipOutputStream.closeEntry();
                    }
                }
                if (dataOutputStream != null) {
                    if (0 != 0) {
                        try {
                            dataOutputStream.close();
                        } catch (Throwable th2) {
                            th.addSuppressed(th2);
                        }
                    } else {
                        dataOutputStream.close();
                    }
                }
                if (zipOutputStream != null) {
                    if (0 != 0) {
                        zipOutputStream.close();
                    } else {
                        zipOutputStream.close();
                    }
                }
            } catch (Throwable th3) {
                try {
                    throw th3;
                } catch (Throwable th4) {
                    if (dataOutputStream != null) {
                        if (th3 != null) {
                            try {
                                dataOutputStream.close();
                            } catch (Throwable th5) {
                                th3.addSuppressed(th5);
                            }
                        } else {
                            dataOutputStream.close();
                        }
                    }
                    throw th4;
                }
            }
        } catch (IOException e) {
            f316a.error(e.getMessage(), e);
        }
    }

    /* JADX WARN: Not initialized variable reg: 10, insn: 0x00e4: MOVE  (r0 I:??[int, float, boolean, short, byte, char, OBJECT, ARRAY]) = (r10 I:??[int, float, boolean, short, byte, char, OBJECT, ARRAY]), block:B:45:0x00e4 */
    /* JADX WARN: Not initialized variable reg: 11, insn: 0x00e9: MOVE  (r0 I:??[int, float, boolean, short, byte, char, OBJECT, ARRAY]) = (r11 I:??[int, float, boolean, short, byte, char, OBJECT, ARRAY]), block:B:47:0x00e9 */
    /* renamed from: a */
    public static String m291a(String str, String str2) {
        InputStream inputStream = null;
        Throwable th;
        try {
            URLConnection openConnection = new URL(str).openConnection();
            openConnection.setConnectTimeout(3000);
            openConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            File m292a = m292a(str2);
            try {
                try {
                    InputStream inputStream2 = openConnection.getInputStream();
                    Throwable th2 = null;
                    FileOutputStream fileOutputStream = new FileOutputStream(m292a);
                    Throwable th3 = null;
                    try {
                        byte[] bArr = new byte[1204];
                        while (true) {
                            int read = inputStream2.read(bArr);
                            if (read == -1) {
                                break;
                            }
                            fileOutputStream.write(bArr, 0, read);
                        }
                        if (fileOutputStream != null) {
                            if (0 != 0) {
                                try {
                                    fileOutputStream.close();
                                } catch (Throwable th4) {
                                    th3.addSuppressed(th4);
                                }
                            } else {
                                fileOutputStream.close();
                            }
                        }
                        if (inputStream2 != null) {
                            if (0 != 0) {
                                try {
                                    inputStream2.close();
                                } catch (Throwable th5) {
                                    th2.addSuppressed(th5);
                                }
                            } else {
                                inputStream2.close();
                            }
                        }
                        return str2;
                    } catch (Throwable th6) {
                        try {
                            throw th6;
                        } catch (Throwable th7) {
                            if (fileOutputStream != null) {
                                if (th6 != null) {
                                    try {
                                        fileOutputStream.close();
                                    } catch (Throwable th8) {
                                        th6.addSuppressed(th8);
                                    }
                                } else {
                                    fileOutputStream.close();
                                }
                            }
                            throw th7;
                        }
                    }
                } catch (Throwable th9) {
                    throw th9;
                }
            } catch (IOException e) {
                f316a.error(e.getMessage(), e);
                return "";
            }
        } catch (IOException e2) {
            f316a.error(e2.getMessage(), e2);
            return "";
        }
    }

    /* renamed from: a */
    public static File m293a(File file) {
        File file2;
        if (!file.exists()) {
            return file;
        }
        File file3 = new File(file.getAbsolutePath());
        File parentFile = file3.getParentFile();
        int i = 1;
        String extension = FilenameUtils.getExtension(file3.getName());
        String baseName = FilenameUtils.getBaseName(file3.getName());
        do {
            int i2 = i;
            i++;
            file2 = new File(parentFile, baseName + "(" + i2 + ")." + extension);
        } while (file2.exists());
        return file2;
    }

    /* renamed from: a */
    private static File m292a(String str) {
        File file = new File(str);
        m288b(file.getParentFile());
        return file;
    }

    /* renamed from: b */
    public static void m288b(File file) {
        if (!file.exists()) {
            file.mkdirs();
        }
    }
}
